﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.IO;
using Newtonsoft.Json;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// wsyS_CodeSet_1 的摘要说明
 /*
	wxyJS V2.0 CS#辅助class
	wxyJS V2.0开发编写者：辛酉戊戌戊寅（网络用户），中国居民身份证号： 230104198110270213。
	wxyJS V2.0开发起始期：2015年1月1日
	wxyJS V2.0开发结束日期：持续更新中……
	wxyJS V2.0遵循开源协议 Apache License 标准。请后续使用者了解Apache License 标准后使用。
	使用提示：
		1、本文件为wxyJS V2.0的C#辅助组成部分，与wxyJS V2.0遵循开源协议 Apache License 标准。
        2、本文件需要拷贝到项目App_Code文件夹下使用。
*/
/// </summary>
public class wsyS_CodeSet_1
{
    public wsyS_CodeSet_1()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }//wsyS_CodeSet_1



    #region C#仿效JS escape函数
    //这       是     一     个     测     试    的     程     序     。    B a S e 6 4  ,    ！    ？     ,  . !    ?    …     …
    //%u8FD9 %u662F %u4E00 %u4E2A %u6D4B %u8BD5 %u7684 %u7A0B %u5E8F %u3002 B a S e 6 4 %2C %uFF01 %uFF1F %2C . %21 %3F %u2026 %u2026
    //%u8FD9 %u662F %u4E00 %u4E2A %u6D4B %u8BD5 %u7684 %u7A0B %u5E8F %u3002BaSe64 %2C %uFF01 %uFF1F %2C. %21 %3F %u2026 %u2026
    public String wxyS_Escape(string s)
    {
        #region C#仿效JS Escape
        StringBuilder sb = new StringBuilder();
        byte[] ba = System.Text.Encoding.Unicode.GetBytes(s);

        for (int i = 0; i < ba.Length; i += 2)
        {
            //Response.Write(ba[i].ToString()+"_");
            if (ba[i + 1] == 0)
            {//数字,大小写字母,以及"/+-*._"不变 
                if ((ba[i] >= 48 && ba[i] <= 57) || (ba[i] >= 64 && ba[i] <= 90) || (ba[i] >= 97 && ba[i] <= 122) || (ba[i] == 42 || ba[i] == 43 || ba[i] == 45 || ba[i] == 46 || ba[i] == 47 || ba[i] == 95))
                {//保持不变 
                    sb.Append(Encoding.Unicode.GetString(ba, i, 2));
                }
                else
                { //%xx形式  
                    sb.Append("%");
                    sb.Append(ba[i].ToString("X2"));
                }
            }
            else
            {
                sb.Append("%u");
                sb.Append(ba[i + 1].ToString("X2"));
                sb.Append(ba[i].ToString("X2"));
            }//if
        }//for(int i=0;i<ba.Length
        return sb.ToString();
        #endregion

        #region  C# 标准Escape
        /* C# Escape
        StringBuilder sb = new StringBuilder();
        byte[] byteArr = System.Text.Encoding.Unicode.GetBytes(s);

        for(int i=0;i<byteArr.Length;i+=2)
        {    
            sb.Append("%u");
            sb.Append(byteArr[i + 1].ToString("X2"));//把字节转换为十六进制的字符串表现形式
            sb.Append(byteArr[i].ToString("X2"));
        }
        return sb.ToString();
        */
        #endregion
    }//wxyS_Escape
    #endregion
    #region C#仿效JS unescape函数
    public String wxyS_Unescape(string s)
    {
        #region C#仿效JS Unescape
        string str = s.Substring(0, 1).ToString() == "%" ? s.Remove(0, 1) : s;//删除最前面一个＂%＂
        string[] strArr1 = str.Split(new string[] { "%" }, StringSplitOptions.None);//以子字符串＂%u＂分隔,StringSplitOptions.None意思是包括空元素
        string[] strArr2 = new string[str.Length * 2];
        byte[] byteArr = new byte[strArr2.Length * 2];
        int i1 = 0;
        int j1 = 0;
        int xz1 = strArr1.Length;
        while (i1 < xz1)
        {
            if (strArr1[i1].Substring(0, 1) == "u" && strArr1[i1].Length == 5)
            {
                byteArr[j1 + 1] = (byte)Convert.ToInt32(strArr1[i1].Substring(1, 2), 16);
                byteArr[j1] = (byte)Convert.ToInt32(strArr1[i1].Substring(3, 2), 16);
                j1 = j1 + 2;
            }
            else
            {
                if (strArr1[i1].Substring(0, 1) == "u" && strArr1[i1].Length != 5)
                {
                    int sllen1 = (strArr1[i1].Length - 5);
                    string strls1 = strArr1[i1].Substring(5, sllen1);
                    byteArr[j1 + 1] = (byte)Convert.ToInt32(strArr1[i1].Substring(1, 2), 16); //把十六进制形式的字串符串转换为二进制字节
                    byteArr[j1] = (byte)Convert.ToInt32(strArr1[i1].Substring(3, 2), 16);
                    j1 = j1 + 2;
                    //Response.Write("<br/>" + strls1.ToString() + "<br/>");
                    for (int x1 = 0; x1 < sllen1; x1++)
                    {
                        byte[] byteArrX1 = System.Text.Encoding.Unicode.GetBytes(strls1.Substring(x1, 1).ToString());
                        byteArr[j1 + 1] = (byte)Convert.ToInt32("0");
                        byteArr[j1] = byteArrX1[0];
                        j1 = j1 + 2;
                    }//for(int
                }
                else
                {
                    if (strArr1[i1].Length > 2)
                    {
                        int sllen1 = (strArr1[i1].Length - 2);
                        string strls1 = strArr1[i1].Substring(2, sllen1);
                        byteArr[j1] = (byte)Convert.ToInt32(strArr1[i1].Substring(0, 2), 16);
                        j1 = j1 + 2;
                        //Response.Write("<br/>" + strls1.ToString() + "<br/>");
                        for (int x1 = 0; x1 < sllen1; x1++)
                        {
                            byte[] byteArrX1 = System.Text.Encoding.Unicode.GetBytes(strls1.Substring(x1, 1).ToString());
                            byteArr[j1 + 1] = (byte)Convert.ToInt32("0");
                            byteArr[j1] = byteArrX1[0];
                            j1 = j1 + 2;
                        }//for(int
                    }
                    else
                    {
                        byteArr[j1 + 1] = (byte)Convert.ToInt32("0");
                        byteArr[j1] = (byte)Convert.ToInt32(strArr1[i1].Substring(0, 2), 16);
                        j1 = j1 + 2;
                    }//if(strArr1[i1].Length > 2)
                }//if(strArr1[i1].Substring(0, 1) == "u" && strArr1[i1].Length != 5)
            }//f(strArr1[i1].Substring(0, 1) == "u" && strArr1[i1].Length == 5)
            i1 = i1 + 1;
        }//while (i1 < xz1)
        byte[] FHbyteArr1 = new byte[j1];
        for (int FH1 = 0; FH1 < FHbyteArr1.Length; FH1++)
        {
            FHbyteArr1[FH1] = byteArr[FH1];
        }//for(int
        string strfh1 = System.Text.Encoding.Unicode.GetString(FHbyteArr1);//把字节转为unicode编码
        return strfh1;

        /*
        Response.Write("</br>");
        for (int xx2 = 0; xx2 < FHbyteArr1.Length; xx2++)
        {
            Response.Write(byteArr[xx2].ToString() + "_");
        }//for(int
        Response.Write("</br>");
         */
        #endregion

        #region C# 标准 Unescape
        /*
        string str = s.Remove(0, 2);//删除最前面两个＂%u＂
        string[] strArr = str.Split(new string[] { "%u" }, StringSplitOptions.None);//以子字符串＂%u＂分隔
        byte[] byteArr = new byte[strArr.Length * 2];
        for (int i = 0, j = 0; i < strArr.Length; i++, j += 2)
        {
            byteArr[j + 1] = Convert.ToByte(strArr[i].Substring(0, 2), 16);  //把十六进制形式的字串符串转换为二进制字节
            byteArr[j] = Convert.ToByte(strArr[i].Substring(2, 2), 16);
        }
        str = System.Text.Encoding.Unicode.GetString(byteArr);//把字节转为unicode编码
        return str;
        */
        #endregion
    }//wxyS_Unescape
    #endregion
    #region Base64加密
    /// </summary>
    /// <param name="codeName">加密采用的编码方式</param>
    /// <param name="source">待加密的明文</param>
    /// <returns></returns>
    public String J_EncodeBase64(Encoding encode, String source)
    {
        string encode1 = "";
        byte[] bytes = encode.GetBytes(source);
        try
        {
            encode1 = Convert.ToBase64String(bytes);
        }
        catch
        {
            encode1 = source;
        }
        return encode1;
    }//J_EncodeBase64

    /// <summary>
    /// Base64加密，采用utf8编码方式加密
    /// </summary>
    /// <param name="source">待加密的明文</param>
    /// <returns>加密后的字符串</returns>
    public String wxyS_EncodeBase64(string source)
    {
        return J_EncodeBase64(Encoding.UTF8, source);
    }//EncodeBase64
    #endregion
    #region Base64解密
    /// </summary>
    /// <param name="codeName">解密采用的编码方式，注意和加密时采用的方式一致</param>
    /// <param name="result">待解密的密文</param>
    /// <returns>解密后的字符串</returns>
    public String J_DecodeBase64(Encoding encode, string result)
    {
        string decode = "";
        byte[] bytes = Convert.FromBase64String(result);
        try
        {
            decode = encode.GetString(bytes);
        }
        catch
        {
            decode = result;
        }
        return decode;
    }//J_DecodeBase64
    /// <summary>
    /// Base64解密，采用utf8编码方式解密
    /// </summary>
    /// <param name="result">待解密的密文</param>
    /// <returns>解密后的字符串</returns>
    public String wxyS_DecodeBase64(string result)
    {
        return J_DecodeBase64(Encoding.UTF8, result);
    }//DecodeBase64
    #endregion
    /*
     * wxyS_EncodeBase64(Str)//加密
     * wxyS_DecodeBase64(Str)//解密
     */


    #region 生成随机字符方案1
    public String wxyS_RandFA1(String Str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int length1 = 1)
    {
        int len = Str.Length - 1;
        String randstr = "";
        Random rd = new Random();
        for (int i = 0; i < length1; i++)
        {
            int num = rd.Next(0,len);
            randstr = randstr + (num+1).ToString() + Str.Substring(num,1);
        }
        return randstr.ToString();
    }//wxyS_RandFA1
    #endregion

    #region 生成随机字符整数字符方案1
    public String wxyS_RandIntFA1(int length1 = 1)
    {
        String randstr = "";
        Random rd = new Random();
        int i = 1;
        while (i<=length1) {
            int num = rd.Next(0,9);
            randstr = randstr.ToString() + num.ToString();
            i = i + 1;
        }//
        return randstr.ToString();
    }//wxyS_RandFA1
    #endregion





}//class wsyS_CodeSet_1